<?php
/**
 * @package   auto_occasion
 * @subpackage 
 * @author    CMS
 * @copyright 2011
 * @license    All rights reserved
 */


/**
 * Classe permettant de gerer les partenaires
 *
 */
class partenaireSrv {

    /**
     * Fonction permettant d'avoir la liste des partenaires
     * @return object $liste
     *
     */
    public function liste () {

        // instanciation de la factory
        $maFactory = jDao::get("commun~ao_partenaire");
        $records   = $maFactory->findAll();

        return $records->fetchAll();
    }

    /**
     * Fonction permettant d'ajouter un partenaire
     * @return object $partenaire
     *
     */
    public function ajouter ($partenaire) {
        // instanciation de la factory
        $maFactory = jDao::get("commun~ao_partenaire");
        $records   = jDao::createRecord("commun~ao_partenaire");

        $records->Part_Libelle      = $partenaire['part_nom'];
        $records->Part_Code         = $partenaire['part_code'];
        $records->Part_Type         = $partenaire['statut'];
        $records->Part_Adresse      = $partenaire['content'];
        $records->Part_CP           = $partenaire['part_cp'];
        $records->Part_Ville        = $partenaire['part_ville'];
        $records->Part_Url          = $partenaire['part_web'];
        $records->Part_Email        = $partenaire['part_mail'];
        $records->Part_Logo         = $partenaire['Part_Logo'];
        $records->Part_Service      = $partenaire['content2']; 
        $records->Part_Repertoire   = self::getRepository($partenaire['part_nom']);
        $records->Part_Presentation = $partenaire['part_texte']; 
        $records->Part_Telephone    = $partenaire['part_phone']; 
        $records->Part_ParutionWeb  = $partenaire['parution']; 
        $records->Part_Nbre_Journal = $partenaire['nbr_jrnal'];
        $records->Part_Nbre_Web     = $partenaire['nbr_web'];
        $records->Part_Nom_Contact  = "";

        if ($maFactory->insert($records)) {
            // ajouter les identifiants de l'amin partenaire
            $adminPartenaire = new stdClass();
            $adminPartenaire->AP_Partenaire = $records->IdPartenaire;
            $adminPartenaire->AP_Login      = $partenaire['part_login'];
            $adminPartenaire->AP_Pwd        = $partenaire['part_mdp'];

            self::addAdminAcces($adminPartenaire);
        }

    }
    
    /**
     * ajouter les identifiants de l'amin partenaire
     * @param $data
     */
     public static function addAdminAcces($data) {
        $maFactory = jDao::get("commun~ao_partenaire_admin");
        $records   = jDao::createRecord("commun~ao_partenaire_admin");

        $records->AP_Partenaire = $data->AP_Partenaire;
        $records->AP_Login      = $data->AP_Login;
        $records->AP_Pwd        = $data->AP_Pwd;

        return $maFactory->insert($records);
     }

    /**
     * modifier les identifiants de l'amin partenaire
     * @param $data
     */
     public static function updateAdminAcces($data) {
        $maFactory = jDao::get("commun~ao_partenaire_admin");
        $records   = $maFactory->get($data->IdAdminPartenaire);

        $records->AP_Partenaire = $data->AP_Partenaire;
        $records->AP_Login      = $data->AP_Login;
        $records->AP_Pwd        = $data->AP_Pwd;

        return $maFactory->update($records);
     }

    /**
     * Création du répertoire
     * @param $name
     */
     public static function getRepository($name) {
        jClasses::inc('commun~toolsStringSrv');
        $name = toolsStringSrv::FormatString($name);
        $path = $GLOBALS['gJConfig']->urlengine['basePath'] . PARTENAIRE_PATH_REPOSITORY . $name;
        $path = $_SERVER['DOCUMENT_ROOT'] . $path;
        //mkdir('c:/Buck Jr.',0755);
        if ( !is_dir($path) ) {
            if(!mkdir ($path, 0755)){
                die("Droits insuffisants") ;
            }
        }

        return $name;
     }

    /**
     * Fonction permettant de modifier un partenaire
     * @return object $partenaire
     *
     */
    public function modifier ($partenaire) {

        // instanciation de la factory
        $maFactory = jDao::get("commun~ao_partenaire");
        $records   = $maFactory->get($partenaire['id']);

        $records->Part_Libelle      = $partenaire['part_nom'];
        $records->Part_Code         = $partenaire['part_code'];
        $records->Part_Type         = $partenaire['statut'];
        $records->Part_Adresse      = $partenaire['content'];
        $records->Part_CP           = $partenaire['part_cp'];
        $records->Part_Ville        = $partenaire['part_ville'];
        $records->Part_Url          = $partenaire['part_web'];
        $records->Part_Email        = $partenaire['part_mail'];
        $records->Part_Logo         = ($partenaire['Part_Logo'])? $partenaire['Part_Logo'] : $partenaire['photoOld'];
        $records->Part_Service      = $partenaire['content2']; 
        $records->Part_Repertoire   = self::getRepository($partenaire['part_nom']);
        $records->Part_Presentation = $partenaire['part_texte']; 
        $records->Part_Telephone    = $partenaire['part_phone']; 
        $records->Part_ParutionWeb  = $partenaire['parution']; 
        $records->Part_Nbre_Journal = $partenaire['nbr_jrnal'];
        $records->Part_Nbre_Web     = $partenaire['nbr_web'];
        $records->Part_Nom_Contact  = "";

        if ($maFactory->update($records)) {
            // ajouter les identifiants de l'amin partenaire
            $adminPartenaire = new stdClass();
            $adminPartenaire->IdAdminPartenaire = $partenaire['IdAdminPartenaire'];
            $adminPartenaire->AP_Partenaire     = $partenaire['id'];
            $adminPartenaire->AP_Login          = $partenaire['part_login'];
            $adminPartenaire->AP_Pwd            = $partenaire['part_mdp'];

            self::updateAdminAcces($adminPartenaire);
        }

        return $maFactory->update($records);
    }

    /**
     * Fonction permettant d'avoir un partenaire à partir de son id
     * @param $id
     * @return object
     *
     */
    public function chargeParId ($id) {
        $maFactory = jDao::get("commun~ao_partenaire");

        $records = $maFactory->getListWithAdmin($id);

        $result = $records->fetchAll();
        return $result[0];
    }

    /**
     * Fonction permettant de supprimer un partenaire
     * @param $id
     * @return object
     *
     */
    public function supprimer ($id) {
        $maFactory = jDao::get("commun~ao_partenaire");

        return  $maFactory->delete($id);
    }

    /**
     * Fonction permettant d'initialiser un objet partenaire
     * @return object
     *
     */
    public function init () {

        $partenaire =  jDao::createRecord("commun~ao_partenaire");
        $partenaire->AP_Login = "";
        $partenaire->IdAdminPartenaire = "";

        return $partenaire;
    }

    /**
     * Fonction permettant d'uploader une image sur le serveur
     * @param array filename
     * @return string filename
     *
     */
    public static function saveUploadedImage ($fileToUpload) {
        
        try {
            jClasses::inc('commun~uploader');

            $uploader = new uploader();
            $uploader->source  = $fileToUpload;
            $uploader->destDir = PARTENAIRE_PATH_LOGO;
            $uploader->upload();

        } catch (exception $e) {
            die($e->getMessage());
        }
        
        return $fileToUpload['name'];
    }

}